package com.qingyu.print.member.handler;



import com.qingyu.common.exception.RRException;
import com.qingyu.common.utils.R;
import com.qingyu.print.member.annotation.DisableBaseResponse;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @author qingyu
 * @create 20210815:48
 */
@ControllerAdvice(basePackages = "com.qingyu.print")
public class GlobalResponseHandler implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return !methodParameter.hasMethodAnnotation(DisableBaseResponse.class);
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        if (o.getClass()==RRException.class) return o;
        return o == null ? new R().ok(): new R().ok().put("data",o);
//        return null;
    }
}
